var G_COOKIE_DOMAN = '.51youpin.com';
function setlasturi(){
	document.cookie = "WANSONSHOP_G_LAST_URI=" + location.href + ";path=/;domain="+G_COOKIE_DOMAN;
}
function get_IDENTIFIER(){
	var strCookie = document.cookie;
	var arrCookie = strCookie.split("; ");
	var arrCookieCount = arrCookie.length;
	var arr,identifyContent = null;
	for(var i = 0; i < arrCookie.length ; i++){
		arr = arrCookie[i].split("=");
		if("WANSONSHOP_IDENTIFIER" == arr[0]){
			var arrStr = document.cookie.split("; ");
			identifyContent = decodeURI(unescape(arr[1]));
			break;
		}
	}
	arrCookie = null;
	if (identifyContent == null) 
		return null;
	else
		return unescape(identifyContent);
}

function set_IDENTIFIER(name){
	var exp  = new Date();
    exp.setTime(exp.getTime() + 24*60*60*1000);
	document.cookie = "WANSONSHOP_IDENTIFIER=" + escape(name) + ";path=/;domain="+G_COOKIE_DOMAN+";expires="+exp.toGMTString();
}

/* 取得客户等级 */
function get_CustomerRankID(){
	var strCookie = document.cookie;
	var arrCookie = strCookie.split("; ");
	var arrCookieCount = arrCookie.length;
	var arr,identifyContent = null;
	var RankID = null;
	for(var i = 0; i < arrCookie.length ; i++){
		arr = arrCookie[i].split("=");
		if("CustomerRankID" == arr[0]){
			var arrStr = document.cookie.split("; ");
			RankID = arr[1];
			break;
		}
	}
	arrCookie = null;
	if (RankID == null) 
		return null;
	else
		return RankID;
}

function contains(parentNode,childNode){   
   return parentNode.contains ? parentNode != childNode && parentNode.contains(childNode) : !!(parentNode.compareDocumentPosition(childNode) & 16);
}

function getEvent(e){   
     return e||window.event;
}

function checkHover(e,target){
    if(getEvent(e).type=="mouseover")
        return !contains(target,getEvent(e).relatedTarget||getEvent(e).fromElement) && !((getEvent(e).relatedTarget||getEvent(e).fromElement)===target);   
    else{   
        return !contains(target,getEvent(e).relatedTarget||getEvent(e).toElement) && !((getEvent(e).relatedTarget||getEvent(e).toElement)===target);   
    }
}

function show_IDENTIFIER(){
	var id = get_IDENTIFIER();
	if (id == null)
	{
		document.writeln('<span>');
		document.writeln('<a class="info_span" href="shop/login.php?act=register" target="_blank">注册</a>');
		document.writeln('</span>');
	    document.writeln('<span>');
		document.writeln('<a class="info_span" href="shop/login.php" target="_blank">登录</a>');
		document.writeln('</span>');
	}
	else
	{
		var RankID = get_CustomerRankID();
		document.writeln('<span class="info_span info_spanie7">');

		document.writeln('<span style="padding-right:2px;"><a id="toolbarUserName" style="color:#333;  display:block; float:left;" href="shop/userCenter.php">'+id+'</a></span>');
		if(RankID==1)
		{
			document.writeln('<img  style="vertical-align:top;" src="http://img.51youpin.com/new2010/theme/xth/images/ucenter/u_pt.gif" alt="" width="16" height="16" /><span style="color:red; padding-left:5px;cursor:pointer" id="userInfoDiv">普通会员</span>');
		}
		else if(RankID==10)
		{
			document.writeln('<img  style="vertical-align:top;" src="http://img.51youpin.com/new2010/theme/xth/images/ucenter/u_vip2.gif" alt="" width="16" height="16" /><span style="color:red; padding-left:5px;cursor:pointer" id="userInfoDiv">VIP会员</span>');
		}
		else if(RankID==20)
		{
			document.writeln('<img  style="vertical-align:top;" src="http://img.51youpin.com/new2010/theme/xth/images/ucenter/u_bj.gif" alt="" width="16" height="16" /><span style="color:red; padding-left:5px;cursor:pointer" id="userInfoDiv">白金会员</span>');
		}
		else if(RankID==30)
		{
			document.writeln('<img  style="vertical-align:top;" src="http://img.51youpin.com/new2010/theme/xth/images/ucenter/u_zs.gif" alt="" width="16" height="16" /><span style="color:red; padding-left:5px;cursor:pointer" id="userInfoDiv">钻石会员</span>');
		}
		else if(RankID==40)
		{
			document.writeln('<img style="vertical-align:top;" src="http://img.51youpin.com/new2010/theme/xth/images/ucenter/u_hg.gif" alt="" width="16" height="16" /><span style="color:red; padding-left:5px; cursor:pointer" id="userInfoDiv">皇冠会员</span>');
		}else if(RankID==50)
		{
			document.writeln('<img style="vertical-align:top;" src="http://img.51youpin.com/new2010/theme/xth/images/ucenter/u_jg.gif" alt="" width="16" height="16" /><span style="color:red; padding-left:5px; cursor:pointer" id="userInfoDiv">金冠会员</span>');
		}
		document.writeln('<span style="padding-left:2px;"><a href="shop/logout.php">退出登陆</a></span>');
		document.writeln('</span>');


		var rankName = jQuery.cookies.get('rankName');
		jQuery('#toolBarRankName').html(rankName);
		
		var rankRate = jQuery.cookies.get('rankRate');
		if(rankRate==100){
			jQuery('#rankRateSpan').css('display', 'none');
		}else{
			jQuery('#toolBarRankRate').html(rankRate);
		}
				
		var pointsRate = jQuery.cookies.get('pointsRate');
		jQuery('#toolBarPointsRate').html(pointsRate);

		var postCustID = jQuery.cookies.get('post_custID');
		jQuery('#toolBarPostCustID').html(postCustID);
		
		var nextRankName = jQuery.cookies.get('nextRankName');
		if(nextRankName == 'topest'){
			jQuery('#nextRankInfo').css('display', 'none');
		}else{
			jQuery('#nextRankName').html(nextRankName);
			var nextNeedAmt = jQuery.cookies.get('nextNeedAmt');
			jQuery('#nextNeedAmt').html('￥'+nextNeedAmt+'元');
	
			var nextRankRate = jQuery.cookies.get('nextRankRate');
			jQuery('#nextRankRate').html(nextRankRate);
	
			var nextPointsRate = jQuery.cookies.get('nextPointsRate');
			jQuery('#nextPointsRate').html(nextPointsRate);

			var head_pic = jQuery.cookies.get('head_pic');

			if(head_pic != 'yp_user_head.gif'){
				jQuery('#topbarUserIcon').attr('src', 'images/head-picture/template/'+head_pic);
			}
		}

		if(rankName && rankRate){
			jQuery("#userInfoDiv").mouseover(function(eve){
				jQuery('#extendUserInfo').css('display','block');
			});

			jQuery("#toolbarUserName").mouseover(function(eve){
				jQuery('#extendUserInfo').css('display','block');
			});
			
			jQuery("#extendUserInfo").mouseout(function(eve){
				if(checkHover(eve,this))
				{
					jQuery('#extendUserInfo').fadeOut('slow');
				}
			});
		}		
	}
}

function browseHistoryMgr(){	
}

browseHistoryMgr.prototype.getHistoryContent = function(){
	var strCookie = document.cookie;
	var arrCookie = strCookie.split("; ");
	var arrCookieCount = arrCookie.length;
	var arr,historyContent = null;
	for(var i = 0; i < arrCookie.length ; i++){
		arr = arrCookie[i].split("=");
		if("WANSONSHOP_BROWSEHISOTRY" == arr[0]){
			historyContent = arr[1];
			break;
		}
	}
	arrCookie = null;
	if (historyContent != null)
		return unescape(historyContent);
	else
		return '';
}
browseHistoryMgr.prototype.setHistoryContent = function(value){
	document.cookie = "WANSONSHOP_BROWSEHISOTRY=" + escape(value) + ";path=/;domain="+G_COOKIE_DOMAN+";";
}
browseHistoryMgr.prototype.getHistoryList = function(){
	var historyContent = this.getHistoryContent();
	if (historyContent == '')
		return null;
	var contentList = historyContent.split("{-}");
	var listCount = contentList.length;
	var contentArray;
	var historyList = new Object();
	var historyItem;
	for(var i = 0; i < listCount; i++){
		if (contentList[i] == '') continue;
		contentArray = contentList[i].split("<>");
		historyItem = new Object();
		historyItem.code = contentArray[0];
		historyItem.name = contentArray[1];
		historyItem.link = contentArray[2];
		historyItem.storePrice = contentArray[3];
		historyItem.img = contentArray[4];
		historyList[historyItem.code] = historyItem;
	}
	return historyList;
};
browseHistoryMgr.prototype.getItem = function(code){
	var historyList = this.getHistoryList();
	if (historyList == null) return null;
	if (historyList[code] == undefined)
		return null;
	return historyList[code];
};
//code<>name<>link<>price<>img
browseHistoryMgr.prototype.add = function(code,name,link,storePrice,img){
	var value = code + '<>' + name + '<>' + link + '<>' + storePrice + '<>' + img;
	var historyContent = this.getHistoryContent();
	var historyItem = this.getItem(code);
	if (historyItem == null){ // add
		this.setHistoryContent(value +  "{-}" + historyContent);
		return true;
	}
	return false;
};
browseHistoryMgr.prototype.clear = function(){
	var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    document.cookie = "WANSONSHOP_BROWSEHISOTRY=0;path=/;domain="+G_COOKIE_DOMAN+";expires=" + exp.toGMTString();
	return true;
};
browseHistoryMgr.prototype.outputHistory = function(){
	var historyList = this.getHistoryList();
	if (historyList == null) return '';
	for (var itemCode in historyList){
		document.writeln('<li>');
		document.writeln('<a href="'+ historyList[itemCode].link +'" target="_blank" title="'+  historyList[itemCode].name +'"><img src="'+ historyList[itemCode].img +'" />');
		document.writeln('<span>'+ historyList[itemCode].name +'</span>');
		document.writeln('<span class="yp_price">￥'+historyList[itemCode].storePrice+'</span></a>');
		document.writeln('</li>');
	}
};
function history_add(code,name,link,storePrice,img){
	var historyMgr = new browseHistoryMgr();
	historyMgr.add(code,name,link,storePrice,img);
	historyMgr = null;
}
function history_ouput(){
	var historyMgr = new browseHistoryMgr();
	historyMgr.outputHistory();
	historyMgr = null;
}
function history_clear(){
	var historyMgr = new browseHistoryMgr();
	historyMgr.clear();
	historyMgr = null;
	document.getElementById('browseHistoryDiv').innerHTML = '';
}
function getUrlParam(name){
	var s = window.location.search;
	if(s){ 
 		var a = s.split("?"); 
 		var b = a[1].split("&"); 
 		for(var i=0; i<b.length; i++){ 
 			var c = b[i].split("=");
 			if (c[0] == name){
 				return c[1];
 			}
		}
	}
}
function Utils(){
	
}
Utils.trim = function(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
Utils.isEmpty = function(val){
  switch (typeof(val)){
    case 'string':
      return Utils.trim(val).length == 0 ? true : false;
      break;
    case 'number':
      return val == 0;
      break;
    case 'object':
      return val == null;
      break;
    case 'array':
      return val.length == 0;
      break;
    default:
      return true;
  }
}

Utils.isNumber = function(val){
  var number;
     if (val==null) return false;
     if (val=="") return false;
     number = new Number(val);
   	 if (!isNaN(number) && val == number.toString())
   	 	return true;
   	 return false;
}

Utils.isInt = function(val){
  if (this.trim(val) == "")
    return false;
  var reg = /\D+/;
  if (!reg.test(val)){
  	var i = parseInt(val);
  	return i.toString() == val;
  }else
  	return false;
}

Utils.isEmail = function(email){
  var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
  return reg1.test( email );
}

Utils.isTel = function (tel){
  var reg = /^[\d|\-|\s|\_]+$/; //只允许使用数字-空格等
  return reg.test( tel );
}
Utils.urlencode = function (str){
	var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };
    var ret = '', unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }

            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
			
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
			
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }

            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

function showPopwin(){
	document.getElementById('fullScreen').style.width = document.documentElement.scrollWidth.toString() + 'px';
	document.getElementById('fullScreen').style.height = document.documentElement.scrollHeight.toString() + 'px';
	document.getElementById('fullScreen').style.display = 'block';
	document.getElementById('popupWin').style.display = 'block';
}

function closePopwin(){
	document.getElementById('fullScreen').style.display = 'none';
	document.getElementById('popupWin').style.display = 'none';
}

function addBookmark(title, url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) {
        return true;
    }
}

function setCookie(name,value){
    var hours = 30;
    var exp  = new Date();
    exp.setTime(exp.getTime() + hours*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;
}

function delCookie(name){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

/* $(document).ready(function(){s
	var currentURL = encodeURIComponent(location.href);
	$.post("/shop/trace.php",{"trace":1,"tracecode":1,"url":currentURL});
}); */
